<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style type="text/css">
        canvas{
            border: 1px solid red;
        }
    </style>
</head>
<body>
<canvas ID="box" width="500" height="500">您的浏览器不支持画布</canvas>

</body>
<script type="text/javascript">
    //通过画布获取画布
    var box=document.getElementById("box");
    var pen=box.getContext("2d");
    //清除画布
    pen.clearRect(0,0,box.width,box.height);
    var ele=pen.createLinearGradient(20,20,400,400);
    ele.addColorStop(0,"red");
    ele.addColorStop(0.5,"green");
    ele.addColorStop(1,"blue");

    pen.beginPath();
    pen.moveTo(20,20);
    pen.lineTo(400,400);
    pen.strokeStyle=ele;
    pen.closePath();
    pen.stroke();
    pen.restore();



    pen.beginPath();
    pen.arc(250,250,200,0,Math.PI*2);
    pen.strokeStyle=ele;
    pen.closePath();
    pen.stroke();


//    pen.beginPath();
//    pen.arc(250,250,200,0,Math.PI*2);
//    pen.fillStyle=ele;
//    pen.fill();
//    pen.closePath();
//    pen.stroke();






</script>
</html>